package com.example.mooctao.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mooctao.R;
import com.example.mooctao.RegisterActivity;

import java.util.Objects;
import java.util.Random;

//验证码登录界面
public class CodeFragment extends Fragment implements View.OnClickListener {

   private EditText ed_phone2;
   private EditText vercode;
   private String mverifyCode;
   private Button login2;
    private Button code;
    //创建视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 初始化数据
        View v= inflater.inflate(R.layout.fragment_code, container, false);
        code=v.findViewById(R.id.btn_vercode);
        ed_phone2=v.findViewById(R.id.et_phone2);
        vercode=v.findViewById(R.id.et_vercode);
        login2=v.findViewById(R.id.btn_login2);
        mverifyCode="456789";
        login2.setOnClickListener(this);
        code.setOnClickListener(this);
        ed_phone2.addTextChangedListener(new HideTextWatcher(ed_phone2,11));
        vercode.addTextChangedListener(new HideTextWatcher(vercode,6));
        code.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View view) {
        String cPhone=ed_phone2.getText().toString();
        if(cPhone.length()<11){
            Toast.makeText(requireActivity(),"请输入正确的手机号",Toast.LENGTH_SHORT).show();
            return;
        }
        switch (view.getId()){
            //获取验证码
            case R.id.btn_vercode:
                mverifyCode = String.format("%06d", new Random().nextInt(999999));
                AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
                builder.setTitle("请记住验证码");
                builder.setMessage("手机号" + cPhone + "，本次验证码是" + mverifyCode + ",请输入验证码");
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
                //登录
            case R.id.btn_login2:
                if(!Objects.equals(mverifyCode, vercode.getText().toString())){
                    Toast.makeText(requireActivity(),"请输入正确的验证码",Toast.LENGTH_SHORT).show();
                    return;
                }

                loginSuccess();
                break;

        }
    }
    //提示用户登录成功
    private void loginSuccess() {
        String desc = String.format("您的手机号码是%s, 恭喜你通过登录验证，点击“确定”按钮返回上个页面", ed_phone2.getText().toString());

        // Create and show the success dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        builder.setTitle("登录成功")
                .setMessage(desc)
                .setPositiveButton("确定", (dialog, which) -> {
                    requireActivity().finish();
                })
                .setNegativeButton("我再看看", null);

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private class HideTextWatcher implements TextWatcher {
        private EditText mView;
        private int mMaxLength;
        private Context mContext;
        public HideTextWatcher(EditText v, int maxLength) {
            this.mView=v;
            this.mMaxLength=maxLength;
            this.mContext = requireActivity();  // 使用 requireActivity 获取上下文
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }
        //在文本内容发生变化之后调用。
        @Override
        public void afterTextChanged(Editable editable) {
            if(editable.toString().length()==mMaxLength){
                //隐藏键盘
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null && mView != null) {
                    imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
                }
            }

        }
    }
}
